package com.taoban.shopdata.thread;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.taoban.dao.HiberOperations;
import com.taoban.dao.mongodb.MongoDbOps;
import com.taoban.shopdata.amazon.AmazoneFetchInfoLoader;
import com.taoban.shopdata.amazon.AmazoneUserInfoPool;
import com.taoban.shopdata.amazon.XmlAmazoneFetchInfoLoader;
import com.taoban.shopdata.common.IsShouldUpdate;
import com.taoban.shopdata.common.MyOwnIsShouldUpdate;
import com.taoban.shopdata.common.MyOwnSaveGoodsImpl;

import com.taoban.shopdata.common.SaveGoods;
import com.taoban.shopdata.entity.AmazonUserInfo;
import com.taoban.shopdata.entity.AmazoneFetchInfo;
import com.taoban.shopdata.entity.AmazoneFetchInfoList;
import com.taoban.shopdata.urlGenerator.ShopDataUrlGenerator;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.DateUtil;
import com.yufei.utils.ExceptionUtil;
import com.yufei.utils.HtmlUtil;
import com.yufei.utils.PatternUtils;
import com.yufei.utils.XPathUtil;

/**
 * @author jasstion
 * 上午11:39:32
 * 获取商品asin然后利用或得到的商品asin以及amazon提供的接口去获取对应的商品的详细信息
 */
public class AmazonGoodsCollectThread implements Runnable{
	private static final Log mLog = LogFactory.getLog(AmazonGoodsCollectThread.class);
final static String mallId="5";
	private AmazonUserInfo amazonUserInfo = null;


	public AmazonGoodsCollectThread() {
		super();
		// TODO Auto-generated constructor stub
	}
   private List<String> fetchAsins(AmazoneFetchInfo amazoneFetchInfo){
	   List<String> allItemids=new ArrayList<>();
		String pagnationUrl=amazoneFetchInfo.getPaginationTemplate();//"http://www.amazon.cn/s/ref=sr_pg_2?rh=n%3A42692071&page=2&ie=UTF8&qid=1365667401";
		List<String> urls=CommonUtil.makeUrlsByPaginationTemplate(pagnationUrl, 0, amazoneFetchInfo.getPageNumberSpace(), amazoneFetchInfo.getFetchedPageNumber());
		if(!pagnationUrl.contains("{}")){
			mLog.info("分页模板配置错误，请重新配置！模板链接是:"+pagnationUrl+"");
			return null;
		}
		List<String> itemIds=null;
		for (String url : urls) {
			try{
			itemIds =PatternUtils.getListStrByRegex(HtmlUtil.getHtmlContent(url, 3, 0, false), amazoneFetchInfo.getAsinRegex());
		 }catch(Exception e){
         	mLog.info("访问"+url+"发送严重错误，错误信息是:"+ExceptionUtil.getExceptionDetailsMessage(e)+"");
         continue;
         }
			allItemids.addAll(itemIds);
		}
	   return allItemids;
   }
   
	@Override
	public void run() {
		// TODO Auto-generated method stub
		amazonUserInfo=AmazoneUserInfoPool.getInstance().getAmazonUserInfo();
		if(amazonUserInfo==null){
			mLog.info("获取不到亚马逊开发者账户信息请正确的配置亚马逊账户信息");
			return;
		}
	//获取asin 
		//获取详细信息
		//解析保存 每个配置文件对应的asins
		List<String> allAsins=null;
		
		
		List<AmazoneFetchInfo> amazoneFetchInfos =amazoneFetchInfoLoader.getAmazoneFetchInfos(); 
		for(AmazoneFetchInfo amazoneFetchInfo :amazoneFetchInfos){
			mLog.info("开始时间为:"+DateUtil.getCurrentDateString()+"");
			temp(amazoneFetchInfo);
			mLog.info("结束时间为:"+DateUtil.getCurrentDateString()+"");
		}
		
        AmazoneUserInfoPool.getInstance().releaseAmazonUserInfo(amazonUserInfo);

		
		
	}
	private void temp(AmazoneFetchInfo amazoneFetchInfo) {
		List<String> allAsins;
		allAsins=this.fetchAsins(amazoneFetchInfo);
		//将asin分组，一组为已存在，一组为新增
		List<String> savedAsins=new ArrayList<>();
		List<String> unSavedAsins=new ArrayList<>();
		for (String asin : allAsins) {
			if(isShouldUpdate!=null){
				if(isShouldUpdate.isShouldUpdate(mallId,asin)){
					savedAsins.add(asin);
				}
				else{
					unSavedAsins.add(asin);
				}
			}
			else{
				//严重错误，程序给出提示直接退出
				mLog.fatal("isShouldUpdate为空，请配置更新策略！");
			}
		
				
			
		}
		
		fetchForSave(unSavedAsins,amazoneFetchInfo.getMyCategoryId());
		fetchForUpdate(savedAsins);
	}

	public void fetchForSave(List<String> unSavedAsins, String categoryId) {
		if (CommonUtil.isEmptyOrNull(unSavedAsins)) {
			return;
		}
		List<List<String>> asinGroups = CommonUtil.splitKs(unSavedAsins, 10);
		String responseXml = null;
		String amazonUrl = null;
		for (List<String> itemIds : asinGroups) {
			amazonUrl = ShopDataUrlGenerator.gernateAmazonUrl(amazonUserInfo,
					"ItemLookup", "Large", itemIds, null);
			try {
				responseXml = HtmlUtil.getHtmlContent(amazonUrl, 2, 5, false);
			} catch (Exception e) {
				mLog.info("访问" + amazonUrl + "发送严重错误，错误信息是:"
						+ ExceptionUtil.getExceptionDetailsMessage(e) + "");
				continue;
			}
			if (CommonUtil.isEmptyOrNull(responseXml)) {
				continue;
			}
			String goodDetailDes = null, goodTitle = null, goodPrice = null, goodUrl = null, asin = null, offerListingId = null;
			Map goodProperties = new HashMap();
			try {
				NodeList itemList = XPathUtil.getNodeListtByXPath(responseXml,
						"//Item");
				for (int i = 0; i < itemList.getLength(); i++) {
					Node itemNode = itemList.item(i);
					asin = XPathUtil.getValueOfNode(itemNode, "ASIN");

					goodTitle = XPathUtil.getValueOfNode(
							XPathUtil.getNode(itemNode, "ItemAttributes"),
							"Title");
					goodDetailDes = XPathUtil.getValueOfNode(itemNode,
							"EditorialReviews");
					if(CommonUtil.isEmptyOrNull(goodDetailDes)){
						//用商品的特性做为描述,查询的时候直接利于lucene分词查询
						//BrowseNodes ItemAttributes
						StringBuffer desc=new StringBuffer();
						desc.append(XPathUtil.getValueOfNode(itemNode,
							"ItemAttributes"))
							.append(XPathUtil.getValueOfNode(itemNode,
							"BrowseNodes"));
						goodDetailDes=desc.toString(
								);
					}
					goodPrice = XPathUtil.getValueOfNode(
							XPathUtil.getNode(itemNode, "OfferSummary"),
							"FormattedPrice");
					goodUrl = XPathUtil.getValueOfNode(itemNode,
							"DetailPageURL");
					mLog.debug("获取商品信息：goodTitle：" + goodTitle + ";goodPrice:"
							+ goodPrice + "");
					goodProperties.put("goodTitle", goodTitle);
					goodProperties.put("goodDetailDes", goodDetailDes);
					goodPrice = formatPrice(goodPrice);
					goodProperties.put("goodPrice", goodPrice);
					goodProperties.put("goodUrl", goodUrl);
					goodProperties.put("mallItemId", asin);
					goodProperties.put("categoryId", categoryId);
					goodProperties.put("mallId", mallId);

					Node node = XPathUtil.getNode(itemNode, "OfferListing");
					if (node != null) {
						offerListingId = XPathUtil.getValueOfNode(node,
								"OfferListingId");

					}
					goodProperties.put("goodPrice", goodPrice);
					goodProperties.put("offerListingId", offerListingId);
					String pictureUrl = null;
					node = XPathUtil.getNode(itemNode, "LargeImage");
					if (node != null) {
						pictureUrl = XPathUtil.getValueOfNode(node, "URL");

					}
					goodProperties.put("pictureUrl", pictureUrl);
					node = XPathUtil.getNode(itemNode, "SmallImage");
					if (node != null) {
						pictureUrl = XPathUtil.getValueOfNode(node, "URL");

					}
					goodProperties.put("SmallImage", pictureUrl);
					node = XPathUtil.getNode(itemNode, "MediumImage");
					if (node != null) {
						pictureUrl = XPathUtil.getValueOfNode(node, "URL");

					}
					goodProperties.put("MediumImage", pictureUrl);

					goodProperties.put("isUpdate", false);

					if (saveGoods != null) {
						try {
							saveGoods.saveGoods(goodProperties);

						} catch (Exception e) {
							mLog.info("保存商品时发生错误，异常信息是"
									+ ExceptionUtil
											.getExceptionDetailsMessage(e) + "");

						}
					}

				}
			} catch (XPathExpressionException | ParserConfigurationException
					| SAXException | IOException e) {
				// TODO Auto-generated catch block
				mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
			}
		}
	}
	private String formatPrice(String goodPrice) {
		goodPrice=goodPrice.replaceAll("￥","");
				goodPrice=goodPrice.replaceAll(",", "");
		return goodPrice;
	}
	public void fetchForUpdate(List<String> savedAsins){
		if(CommonUtil.isEmptyOrNull(savedAsins)){
			return;
		}
		//仅仅更新price,库存
		List<List<String>> asinGroups=CommonUtil.splitKs(savedAsins, 10);
		String responseXml=null;
		String amazonUrl=null;
		for (List<String> itemIds : asinGroups) {
			
			amazonUrl=ShopDataUrlGenerator.gernateAmazonUrl(amazonUserInfo, "ItemLookup", "OfferFull", itemIds, null);
            try{
			responseXml=HtmlUtil.getHtmlContent(amazonUrl, 2, 0, false);
		 }catch(Exception e){
	         	mLog.info("访问"+amazonUrl+"发送严重错误，错误信息是:"+ExceptionUtil.getExceptionDetailsMessage(e)+"");
	         continue;
	         }
			String goodPrice=null,offerListingId=null,asin=null;
			Map goodProperties=new HashMap();
			try {
				NodeList itemList=XPathUtil.getNodeListtByXPath(responseXml, "//Item");
 				for (int i = 0; i < itemList.getLength(); i++) {
					Node itemNode=itemList.item(i);
					asin=XPathUtil.getValueOfNode(itemNode, "ASIN");
					

				
					goodPrice=XPathUtil.getValueOfNode(XPathUtil.getNode(itemNode, "OfferSummary"), "FormattedPrice"); 
					Node node = XPathUtil.getNode(itemNode, "OfferListing");
					if(node!=null){
						offerListingId=XPathUtil.getValueOfNode(node, "OfferListingId");

					}					
					goodPrice = formatPrice(goodPrice);

					goodProperties.put("goodPrice", goodPrice);
					goodProperties.put("offerListingId", offerListingId);
					goodProperties.put("mallItemId", asin);
					goodProperties.put("mallId", mallId);
					goodProperties.put("isUpdate", true);
					mLog.debug("update商品信息：goodPrice："+goodPrice+";offerListingId:"+offerListingId+"");
					
					

					   if(saveGoods!=null){
	                    	try{
	                        	saveGoods.saveGoods(goodProperties);

	                    	}catch(Exception e){
	                    		mLog.info("保存商品时发生错误，异常信息是"+ExceptionUtil.getExceptionDetailsMessage(e)+"");
	                    	    
	                    	}
	                    }

					
					
				}
			} catch (XPathExpressionException | ParserConfigurationException
					| SAXException | IOException e) {
				// TODO Auto-generated catch block
				mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
			}
		}
	}
	public IsShouldUpdate isShouldUpdate=null; 
	public SaveGoods saveGoods=null;
	public AmazoneFetchInfoLoader amazoneFetchInfoLoader;
	public AmazoneFetchInfoLoader getAmazoneFetchInfoLoader() {
		return amazoneFetchInfoLoader;
	}
	public void setAmazoneFetchInfoLoader(
			AmazoneFetchInfoLoader amazoneFetchInfoLoader) {
		this.amazoneFetchInfoLoader = amazoneFetchInfoLoader;
	}
	public IsShouldUpdate getIsShouldUpdate() {
		return isShouldUpdate;
	}
	public void setIsShouldUpdate(IsShouldUpdate isShouldUpdate) {
		this.isShouldUpdate = isShouldUpdate;
	}
	public SaveGoods getSaveGoods() {
		return saveGoods;
	}
	public void setSaveGoods(SaveGoods saveGoods) {
		this.saveGoods = saveGoods;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String pagnationUrl="http://www.amazon.cn/s/ref=sr_pg_3?rh=n%3A888465051%2Ck%3A%E7%AC%94%E8%AE%B0%E6%9C%AC%2Cn%3A106200071&page=4&bbn=888465051&keywords=%E7%AC%94%E8%AE%B0%E6%9C%AC&ie=UTF8&qid=1365665094";
		String pagnationUrl="http://www.amazon.cn/s/ref=sr_pg_2?rh=n%3A888465051%2Cn%3A106200071&page=2&ie=UTF8&qid=1365734164";
	/*	List<String> allItemids=PatternUtils.getListStrByRegex(HtmlUtil.getHtmlContent(pagnationUrl, 3, 0, false), "/dp/(.*?)/");
	   List<List<String>> itemIdGroups=CommonUtil.splitKs(allItemids, 10);
	 
		   List<String> itemIds=itemIdGroups.get(0);
		String amazonUrl=ShopDataUrlGenerator.gernateAmazonUrl(ShopDataUrlGenerator.getRandomUserInfo(), "ItemLookup", "Large", itemIds, null);
			String htmlContent=HtmlUtil.getHtmlContent(amazonUrl, 2, 0, false);
			System.out.print(htmlContent);*/
		AmazonGoodsCollectThread amazonGoodsCollectThread = new AmazonGoodsCollectThread();
		amazonGoodsCollectThread.setAmazoneFetchInfoLoader(new XmlAmazoneFetchInfoLoader());
		amazonGoodsCollectThread.setSaveGoods(new MyOwnSaveGoodsImpl());
	    amazonGoodsCollectThread.setIsShouldUpdate(new MyOwnIsShouldUpdate());
		//amazonGoodsCollectThread.run();
		//new TMallSpProductCollectThread().run();
		ScheduledExecutorService scheduledExecutorService=Executors.newScheduledThreadPool(2);
		//定期想spider-server 获取价格更行任务
		scheduledExecutorService.scheduleAtFixedRate(amazonGoodsCollectThread, 1,60*60*6, TimeUnit.SECONDS);
		
		
	   
      
	}
}
